package com.mall.users.controller;

import com.mall.commons.utils.CommonResult;
import com.mall.model.domain.UmsMenu;
import com.mall.model.domain.UmsResource;
import com.mall.model.domain.UmsRole;
import com.mall.model.vo.CommonPage;
import com.mall.users.service.UmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class UmsRoleController {


    @Autowired
    private UmsRoleService umsRoleService;

    /**
     * 根据角色名称分页获取角色列表
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             String keyword) {
        CommonPage page = umsRoleService.list(pageNum,pageSize, keyword);
        return CommonResult.success(page);
    }

    /**
     * 根据角色名称分页获取角色列表
     * @return
     */
    @GetMapping("/listAll")
    public CommonResult listAll() {
        List<UmsRole> umsRoleList = umsRoleService.listAll();
        return CommonResult.success(umsRoleList);
    }

    /**
     * 修改角色
     * @param id
     * @param umsRole
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody UmsRole umsRole) {
        umsRoleService.update(umsRole);
        return CommonResult.success(1);
    }

    /**
     * 添加角色
     * @param umsRole
     * @return
     */
    @PostMapping("create")
    public CommonResult update(@RequestBody UmsRole umsRole) {
        umsRoleService.save(umsRole);
        return CommonResult.success(1);
    }

    /**
     * 批量删除角色
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public CommonResult delete(@RequestParam List<Long> ids) {
        umsRoleService.delete(ids);
        return CommonResult.success(1);
    }


    /**
     * 获取角色相关菜单
     * @param roleId
     * @return
     */
    @GetMapping("/listMenu/{roleId}")
    public CommonResult listMenu(@PathVariable Long roleId) {
        List<UmsMenu> menuList = umsRoleService.listMenu(roleId);
        return CommonResult.success(menuList);
    }

    /**
     * 获取角色相关资源
     * @param roleId
     * @return
     */
    @GetMapping("/listResource/{roleId}")
    public CommonResult listResource(@PathVariable Long roleId) {
        List<UmsResource> resourceList = umsRoleService.listResource(roleId);
        return CommonResult.success(resourceList);
    }

    /**
     * 给角色分配菜单
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("/allocMenu")
    public CommonResult allocMenu(Long roleId, @RequestParam List<Long> menuIds) {
        umsRoleService.allocMenu(roleId, menuIds);
        return CommonResult.success(1);
    }

    /**
     * 修改角色状态
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public CommonResult updateStatus(@PathVariable Long id, Integer status) {
        umsRoleService.updateStatus(id,status);
        return CommonResult.success(1);
    }

    /**
     * 给角色分配资源
     * @param roleId
     * @param resourceIds
     * @return
     */
    @PostMapping("/allocResource")
    public CommonResult allocResource(Long roleId, @RequestParam List<Long> resourceIds) {
        umsRoleService.allocResource(roleId, resourceIds);
        return CommonResult.success(1);
    }
}
